#! /usr/local/bin/rake
#       = ============================================================================
# A set of rake tasks for Terminal launches.
#       = ============================================================================
require 'rubygems'
require 'rbosa' #sudo gem install rubyosa
require 'osx/aeosa' # http://rubycocoa.sourceforge.net/files/rubyaeosa-0.2.3.tgz



# online docs 
# http://www.fobj.com/rubyaeosa/
# http://rubyosa.rubyforge.org/
# Source for rubyaeosa
# http://rubycocoa.sourceforge.net/files/rubyaeosa-0.2.3.tgz

@app = OSA.app('Terminal')
@finder = OSA.app('Finder')
@app.merge('StandardAdditions')
@app.activate  

# bounds = @finder.desktop.bounds # does not work, trying to grab bounds of desktop via applevents

#screen dimentions, default to macbook pro size
@width = 1440 
@height = 900 - 20
#how many rows and columns
@rows = 4
@cols = 2


#remote server config
@server_uri = "0.0.0.0"
@user_name = "jason"
@prod =  [1234,4321] #these are port numbers
@dev = [5432] #these are port numbers
@mongrels = [5000,5001,5002]#these are mongrel pids

@stack = []



def do_script(command = "", window = nil) # if we can do away with this method, we can loose rubyaeosa
script = <<SCRIPT
tell application "Terminal"
do script "#{command}"  #{ "in window #{window}" if window  }
end tell
SCRIPT
OSX.do_osascript(script)
end


def grid_position(row, col)
	row = (row + 1)
	width = (@width / @cols)
	height = (@height / @rows)
	x = (width * col)
	y = (@height - (height * row))
	return [x, y, width, height]
end
def login(slice)
	@app.do_script "ssh #{@user_name}@#{@server_uri} -p#{slice}" 
	sleep 0.25
end
def open_terminals()
	@stack.reverse!
	@cols.times do |i|
		@rows.times do |j|
			break unless (@stack.pop.call(j,i) rescue nil)
		end
	end
end

namespace :tm do
	
	desc "Locals"
	task  (:locals) do 
				
		@stack << Proc.new do |i,j|
			@app.do_script "script/server --debugger"
			@app.windows[0].frame= grid_position(i,j) 
			@app.windows[0].custom_title= "LOCAL DEV TAIL" 
		end
		@stack << Proc.new do |i,j|
			@app.do_script "script/server -eproduction_test -p3001"
			@app.windows[0].frame= grid_position(i,j) 
			@app.windows[0].custom_title= "LOCAL PROD TAIL" 
		end
		@stack << Proc.new do |i,j|
			@app.do_script "svn update"
			@app.windows[0].frame= grid_position(i,j) 
			@app.windows[0].custom_title= "SVN" 
		end
		@stack << Proc.new do |i,j|
			@app.do_script ""
			@app.windows[0].frame= grid_position(i,j) 
			@app.windows[0].custom_title= "LOCAL TERM" 
		end
		@stack << Proc.new do |i,j|
			@app.do_script "script/console"
			@app.windows[0].frame= grid_position(i,j) 
			@app.windows[0].custom_title= "LOCAL CONSOLE" 
		end
		open_terminals
	end #end locals	
	desc "prod_test"
	task  (:prod_test) do 
				
		@stack= @stack + @dev.map do |c| 
	    Proc.new do |i,j|
	      login(c)
				do_script "cd /data/#{@user_name}_prod_test/current/" , 0
	      do_script "tail -f /data/#{@user_name}_prod_test/current/log/production_test.log" , 0
	      @app.windows[0].frame= grid_position(i,j) 
	      @app.windows[0].custom_title= "PROD_TEST TAIL #{c}" 
	    end 
	  end
		open_terminals
	end #end prod_test
	desc "devs"
	task  (:devs) do 
		@stack= @stack + @dev.map do |c| 
			Proc.new do |i,j|
				login(c)
				do_script "cd /data/#{@user_name}_stage/current/" , 0
				do_script "tail -f /data/#{@user_name}_stage/current/log/development.log" , 0
				
				@app.windows[0].frame= grid_position(i,j) 
				@app.windows[0].custom_title= "DEV TAIL #{c}" 
			end 
		end
		@stack= @stack + @dev.map do |c| 
			Proc.new do |i,j|
				login c
				do_script "cd /data/#{@user_name}_stage/current/" , 0
				do_script "top" , 0
				sleep 0.5
				do_script "M" , 0
				@app.windows[0].frame= grid_position(i,j) 
				@app.windows[0].custom_title= "DEV TOP #{c}"
			end 
		end
		@stack= @stack + @dev.map do |c| 
			Proc.new do |i,j|
				login c
				do_script "cd /data/#{@user_name}_stage/current/" , 0
				sleep 0.25
				do_script "vmstat 5" , 0
				@app.windows[0].frame= grid_position(i,j) 
				@app.windows[0].custom_title= "DEV VMSTAT #{c}" 
			end 
		end
		open_terminals
	end #end devs
	namespace :prod do
		desc "vmstats"
		task  (:vmstats) do 
			#VMSTATS
			@stack= @stack + @prod.map do |c| 
				Proc.new do |i,j|
					login c
					sleep 0.25
					do_script "vmstat 5" , 0
					@app.windows[0].frame= grid_position(i,j) 
					@app.windows[0].custom_title= "PROD VMSTAT #{c}" 
				end 
			end
			open_terminals
		end
		desc "tail_monits"
		task  (:tail_monits) do 
			# TAILS
			@stack= @stack + @prod.map do |c| 
				Proc.new do |i,j|
					login(c)
					do_script "tail -f /data/#{@user_name}/current/log/monit.log" , 0
				
					@app.windows[0].frame= grid_position(i,j) 
					@app.windows[0].custom_title= "PROD MONIT TAIL #{c}" 
				end 
			end
			open_terminals
		end
		desc "tops"
		task  (:tops) do 
			#TOPS
			@stack= @stack + @prod.map do |c| 
				Proc.new do |i,j|
					login c
					do_script "top" , 0
					sleep 3
					do_script "M" , 0
					@app.windows[0].frame= grid_position(i,j) 
					@app.windows[0].custom_title= "PROD TOP #{c}" 
				end 
			end
			open_terminals
		end
		desc "tail_nginx"
		task  (:tail_nginx) do 
		  # TAILS
		  @stack= @stack + @prod.map do |c| 
		    Proc.new do |i,j|
		      login(c)
		      do_script "tail -f /var/log/nginx.vhost.access.log" , 0
		      @app.windows[0].frame= grid_position(i,j) 
		      @app.windows[0].custom_title= "PROD NGINX TAIL #{c}" 
		    end 
		  end
		open_terminals
		end
		desc "tails"
		task  (:tails) do 
		  # TAILS
		  @stack= @stack + @prod.map do |c| 
		    Proc.new do |i,j|
		      login(c)
					do_script "cd /data/#{@user_name}/current/" , 0
		      do_script "tail -f /data/#{@user_name}/current/log/production.log" , 0
					@app.windows[0].frame= grid_position(i,j) 
		      @app.windows[0].custom_title= "PROD TAIL #{c}" 
		    end 
		  end
		open_terminals
		end
	end #end  prod namespace 
end #end  tm namespace